package cdn_cmd

import (
	"context"
	"fmt"
	"time"

	my_common "atomgit.com/openlinksaas-org/tencloud/common"
	"github.com/spf13/cobra"
	cdn_client "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cdn/v20180606"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
	"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
)

var listDomainCmdDomainList = []string{}

var _listDomainCmd = &cobra.Command{
	Use: "listDomain",
	Run: func(cmd *cobra.Command, args []string) {
		client, err := cdn_client.NewClient(common.NewCredential(my_common.CmdSecretId, my_common.CmdSecretKey), my_common.CmdRegion, profile.NewClientProfile())
		if err != nil {
			my_common.OutputError(err)
		}
		ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
		defer cancel()
		req := cdn_client.NewDescribeDomainsRequest()
		if len(listDomainCmdDomainList) > 0 {
			name := "domain"
			valueList := []*string{}
			for _, domain := range listDomainCmdDomainList {
				valueList = append(valueList, &domain)
			}
			fuzzy := false
			req.Filters = []*cdn_client.DomainFilter{
				{
					Name:  &name,
					Value: valueList,
					Fuzzy: &fuzzy,
				},
			}
		}

		res, err := client.DescribeDomainsWithContext(ctx, req)
		if err != nil {
			my_common.OutputError(err)
		}
		fmt.Println(res.ToJsonString())
	},
}

func init() {
	_listDomainCmd.Flags().StringArrayVar(&listDomainCmdDomainList, "domain", []string{}, "filter by domain")
}
